home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdTextAreaProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  5.9 KB  |  188 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Text Area Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var insertNew;
  38. var textareaElement;
  39.  
  40. // dialog initialization code
  41.  
  42. function Startup()
  43. {
  44.   if (!InitEditorShell())
  45.     return;
  46.  
  47.   gDialog = {
  48.     accept:             document.documentElement.getButton("accept"),
  49.     textareaName:       document.getElementById("TextAreaName"),
  50.     textareaRows:       document.getElementById("TextAreaRows"),
  51.     textareaCols:       document.getElementById("TextAreaCols"),
  52.     textareaWrap:       document.getElementById("TextAreaWrap"),
  53.     textareaReadOnly:   document.getElementById("TextAreaReadOnly"),
  54.     textareaDisabled:   document.getElementById("TextAreaDisabled"),
  55.     textareaTabIndex:   document.getElementById("TextAreaTabIndex"),
  56.     textareaAccessKey:  document.getElementById("TextAreaAccessKey"),
  57.     textareaValue:      document.getElementById("TextAreaValue"),
  58.     MoreSection:        document.getElementById("MoreSection"),
  59.     MoreFewerButton:    document.getElementById("MoreFewerButton")
  60.   };
  61.  
  62.   // Get a single selected text area element
  63.   var tagName = "textarea";
  64.   textareaElement = editorShell.GetSelectedElement(tagName);
  65.  
  66.   if (textareaElement) {
  67.     // We found an element and don't need to insert one
  68.     insertNew = false;
  69.  
  70.     gDialog.textareaValue.value = textareaElement.value;
  71.   }
  72.   else
  73.   {
  74.     insertNew = true;
  75.  
  76.     // We don't have an element selected,
  77.     //  so create one with default attributes
  78.  
  79.     textareaElement = editorShell.CreateElementWithDefaults(tagName);
  80.     if (!textareaElement)
  81.     {
  82.       dump("Failed to get selected element or create a new one!\n");
  83.       window.close();
  84.       return;
  85.     }
  86.     else
  87.       gDialog.textareaValue.value = GetSelectionAsText();
  88.   }
  89.  
  90.   // Make a copy to use for AdvancedEdit
  91.   globalElement = textareaElement.cloneNode(false);
  92.  
  93.   InitDialog();
  94.  
  95.   InitMoreFewer();
  96.  
  97.   SetTextboxFocus(gDialog.textareaName);
  98.   
  99.   SetWindowLocation();
  100. }
  101.  
  102. function InitDialog()
  103. {
  104.   gDialog.textareaName.value = globalElement.getAttribute("name");
  105.   gDialog.textareaRows.value = globalElement.getAttribute("rows");
  106.   gDialog.textareaCols.value = globalElement.getAttribute("cols");
  107.   gDialog.textareaWrap.value = GetHTMLOrCSSStyleValue(globalElement, "wrap", "white-space");
  108.   gDialog.textareaReadOnly.checked = globalElement.hasAttribute("readonly");
  109.   gDialog.textareaDisabled.checked = globalElement.hasAttribute("disabled");
  110.   gDialog.textareaTabIndex.value = globalElement.getAttribute("tabindex");
  111.   gDialog.textareaAccessKey.value = globalElement.getAttribute("accesskey");
  112.   onInput();
  113. }
  114.  
  115. function onInput()
  116. {
  117.   var disabled = !gDialog.textareaName.value || !gDialog.textareaRows.value || !gDialog.textareaCols.value;
  118.   if (gDialog.accept.disabled != disabled)
  119.     gDialog.accept.disabled = disabled;
  120. }
  121.  
  122. function ValidateData()
  123. {
  124.   var attributes = {
  125.     name: gDialog.textareaName.value,
  126.     rows: gDialog.textareaRows.value,
  127.     cols: gDialog.textareaCols.value,
  128.     wrap: gDialog.textareaWrap.value,
  129.     tabindex: gDialog.textareaTabIndex.value,
  130.     accesskey: gDialog.textareaAccessKey.value
  131.   };
  132.   var flags = {
  133.     readonly: gDialog.textareaReadOnly.checked,
  134.     disabled: gDialog.textareaDisabled.checked
  135.   };
  136.   for (var a in attributes)
  137.   {
  138.     if (attributes[a])
  139.       globalElement.setAttribute(a, attributes[a]);
  140.     else
  141.       globalElement.removeAttribute(a);
  142.   }
  143.   for (var f in flags)
  144.   {
  145.     if (flags[f])
  146.       globalElement.setAttribute(f, "");
  147.     else
  148.       globalElement.removeAttribute(f);
  149.   }
  150.   return true;
  151. }
  152.  
  153. function onAccept()
  154. {
  155.   // All values are valid - copy to actual element in doc or
  156.   //   element created to insert
  157.   ValidateData();
  158.  
  159.   editorShell.BeginBatchChanges();
  160.  
  161.   try {
  162.     // undoably set value
  163.     var initialText = gDialog.textareaValue.value;
  164.     if (initialText != textareaElement.value) {
  165.       while (textareaElement.hasChildNodes())
  166.         editorShell.editor.deleteNode(textareaElement.firstChild);
  167.       if (initialText) {
  168.         var textNode = editorShell.editorDocument.createTextNode(initialText);
  169.         editorShell.editor.insertNode(textNode, textareaElement, 0);
  170.       }
  171.     }
  172.  
  173.     if (insertNew)
  174.       editorShell.InsertElementAtSelection(textareaElement, true);
  175.     else
  176.       editorShell.SelectElement(textareaElement);
  177.  
  178.     editorShell.CloneAttributes(textareaElement, globalElement);
  179.   } finally {
  180.     editorShell.EndBatchChanges();
  181.   }
  182.  
  183.   SaveWindowLocation();
  184.  
  185.   return true;
  186. }
  187.  
  188.